//
//  NewProductViewController.m
//  Beauty
//
//  Created by Snow on 16/9/9.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "NewProductViewController.h"
#import "GXMacro.h"
#import "ModelOfNewProductDetail.h"
#import "GXCycleImage.h"
#import "GXNetWorkingParser.h"
#import "UIView+Frame.h"
#import "DetailNewProductTableViewCell.h"
#import "ZWYNetTool.h"
#import "LoginViewController.h"
#import "WebViewCellOfGood.h"
@interface NewProductViewController ()<UITableViewDelegate, UITableViewDataSource, GXCycleImageDelegate, DetailNewProductDelegate>
@property (nonatomic, strong) UITableView *tableView;
//轮播图
@property (nonatomic, strong) NSMutableArray *arrayOfGoodPic;
//店铺小图标
@property (nonatomic, strong)  UIImageView *image;
//数据数组
@property (nonatomic, strong) NSMutableArray *arrayOfData;
@property (nonatomic, strong) UILabel *labelOfCount;
@property (nonatomic, assign) BOOL isHaveSave;

@end

static CGFloat height = 0;
@implementation NewProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
}
- (void)handleData
{
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    NSString *password = [userDefault objectForKey:@"password"];
    NSLog(@"%@", password);
    NSLog(@"%@", _goods_id);
    _arrayOfGoodPic = [NSMutableArray array];
    _arrayOfData = [NSMutableArray array];
    NSString *url = [NSString string];
    if (name.length != 0) {
        NSString *urlString = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_detail_1_5&goods_id=%@&key=%@", _goods_id, password];
        url = urlString;
    } else {
        NSString *urlString = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_detail_1_5&goods_id=%@", _goods_id];
        url = urlString;
    }
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        ModelOfNewProductDetail *model = [[ModelOfNewProductDetail alloc] init];
        [model setValuesForKeysWithDictionary:responseObject[@"datas"]];
        [_arrayOfData addObject:model];
        _arrayOfGoodPic = [model.goods_image componentsSeparatedByString:@","].mutableCopy;
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}

- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[DetailNewProductTableViewCell class] forCellReuseIdentifier:@"DetailNewProductTableViewCell"];
    [_tableView registerClass:[WebViewCellOfGood class] forCellReuseIdentifier:@"WebViewCellOfGood"];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:buttonBack];
    [buttonBack setImage:[UIImage imageNamed:@"buttonOfBack"] forState:UIControlStateNormal];
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).mas_equalTo(20);
        make.top.mas_equalTo(self.view.mas_top).mas_equalTo(50);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    [buttonBack addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    UIView *viewOfBottom = [[UIView alloc] init];
    [self.view addSubview:viewOfBottom];
    viewOfBottom.backgroundColor = [UIColor whiteColor];
    [viewOfBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.bottom.mas_equalTo(self.view.mas_bottom).mas_equalTo(49);
        make.height.mas_equalTo(64);
    }];
    UIButton *buttonOfBuy = [UIButton buttonWithType:UIButtonTypeCustom];
    [viewOfBottom addSubview:buttonOfBuy];
    buttonOfBuy.backgroundColor =[UIColor colorWithRed:0.110 green:0.102 blue:0.196 alpha:0.791];
    [buttonOfBuy setImage:[UIImage imageNamed:@"buttonOfWallet_1"] forState:UIControlStateNormal];
    [buttonOfBuy setTitle:@"立即购买" forState:UIControlStateNormal];
    [buttonOfBuy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOfBottom.mas_left).mas_equalTo(10);
        make.top.mas_equalTo(viewOfBottom.mas_top).mas_equalTo(10);
        make.right.mas_equalTo(viewOfBottom.mas_right).mas_equalTo(-100);
        make.bottom.mas_equalTo(viewOfBottom.mas_bottom).mas_equalTo(-10);
    }];
    
    UIButton *buttonOfAddToBag = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:buttonOfAddToBag];
    [buttonOfAddToBag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(buttonOfBuy.mas_right).mas_equalTo(10);
        make.top.mas_equalTo(buttonOfBuy.mas_top);
        make.right.mas_equalTo(self.view.mas_right).mas_equalTo(-10);
        make.bottom.mas_equalTo(buttonOfBuy.mas_bottom);
    }];
    [buttonOfAddToBag setImage:[UIImage imageNamed:@"buttonOfBag"] forState:UIControlStateNormal];
    buttonOfAddToBag.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:0.802];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 1 || section == 4) {
        return 0;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfNewProductDetail *model = [_arrayOfData firstObject];
    NSString *str = model.goods_common_info[@"goods_body"];
    _isHaveSave = model.IsHaveBuy;
    if (indexPath.section == 2) {
        WebViewCellOfGood *cell = [tableView dequeueReusableCellWithIdentifier:@"WebViewCellOfGood"];
        cell.webString = str;
        
        cell.webBlock = ^(UIWebView *webView) {
        
            height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
        };
        return cell;
    } else {
    DetailNewProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailNewProductTableViewCell"];
    cell.delegate = self;
    if (_arrayOfData.count != 0) {
        ModelOfNewProductDetail *model = [_arrayOfData firstObject];
        cell.array = model.goods_commend_list;
    }
    
    return cell;
    }
}
//协议-点击方法
- (void)getGoodDetailWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 662;
    } else if (section == 1) {
        return 80;
    } else if(section == 3) {
        return 60;
    } else if(section == 4) {
        return 300;
    } else {
        return 40;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return 150;
    } else if(indexPath.section == 2){
        return height;
    } else {
        return 100;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(_arrayOfData.count != 0) {
    ModelOfNewProductDetail *model = [_arrayOfData objectAtIndex:0];
    if (section == 0) {
        NSDictionary *dicOfGood = model.goods_info;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 500)];
        view.backgroundColor = [UIColor whiteColor];
        //轮播图
        GXCycleImage *images = [[GXCycleImage alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        images.delegate = self;
        [view addSubview:images];
        if (_arrayOfGoodPic.count != 0) {
            images.arrayImage = _arrayOfGoodPic;
        }
        //心愿单按钮
        UIView *viewOfwish = [[UIView alloc] initWithFrame:CGRectMake(10, 310, 120, 30)];
        viewOfwish.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        [view addSubview:viewOfwish];
        viewOfwish.layer.masksToBounds = YES;
        viewOfwish.layer.cornerRadius = 5;
        UILabel *labelOfwish = [[UILabel alloc] initWithFrame:CGRectMake(35, 5, 80, 20)];
        labelOfwish.text = @"加入心愿单";
        labelOfwish.font = [UIFont systemFontOfSize:14];
        [viewOfwish addSubview:labelOfwish];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 20, 20)];
        image.image = [UIImage imageNamed:@"buttonOfLove"];
        [viewOfwish addSubview:image];
        UIImageView *imageOfCount = [[UIImageView alloc] initWithFrame:CGRectMake(140, 305, 40, 40)];
        imageOfCount.image = [UIImage imageNamed:@"detailCount"];
        [view addSubview:imageOfCount];
        _labelOfCount = [[UILabel alloc] initWithFrame:imageOfCount.bounds];
        [imageOfCount addSubview:_labelOfCount];
        _labelOfCount.text = [dicOfGood objectForKey:@"favorites_count"];
        _labelOfCount.textAlignment = 1;
        //点击方法
        UITapGestureRecognizer *tapWish = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleWish:)];
        [viewOfwish addGestureRecognizer:tapWish];
        
        //加入购物袋按钮
        UIButton *imageOfBag = [UIButton buttonWithType:UIButtonTypeCustom];
        imageOfBag.layer.masksToBounds = YES;
        imageOfBag.layer.cornerRadius = 5;
        imageOfBag.frame = CGRectMake(SCREEN_WIDTH - 150, 310, 30, 30);
        [imageOfBag setImage:[UIImage imageNamed:@"buttonOfBag_1"] forState:UIControlStateNormal];
        imageOfBag.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        [view addSubview:imageOfBag];
        //分享按钮
        UIButton *buttonOfShare = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonOfShare.layer.masksToBounds = YES;
        buttonOfShare.layer.cornerRadius = 5;
        buttonOfShare.frame = CGRectMake(SCREEN_WIDTH - 100, 310, 30, 30);
        [buttonOfShare setImage:[UIImage imageNamed:@"buttonOfShares"] forState:UIControlStateNormal];
        buttonOfShare.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        [view addSubview:buttonOfShare];
        //客服按钮
        UIButton *buttonOfTel = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonOfTel.layer.masksToBounds = YES;
        buttonOfTel.layer.cornerRadius = 5;
        buttonOfTel.frame = CGRectMake(SCREEN_WIDTH - 50, 310, 30, 30);
        [buttonOfTel setImage:[UIImage imageNamed:@"buttonOfPeople"] forState:UIControlStateNormal];
        buttonOfTel.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        [view addSubview:buttonOfTel];
        //line
        UIView *viewOfLine = [[UIView alloc] initWithFrame:CGRectMake(0, 350, SCREEN_WIDTH, 1)];
        viewOfLine.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
        [view addSubview:viewOfLine];
        //name
        UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(10, 360, SCREEN_WIDTH - 20, 60)];
        labelOfName.text = [dicOfGood objectForKey:@"goods_name"];
        labelOfName.numberOfLines = 0;
        labelOfName.font = [UIFont systemFontOfSize:20];
        [view addSubview:labelOfName];
        //price
        UILabel *labelOfPrice = [[UILabel alloc] initWithFrame:CGRectMake(10, 420, 100, 50)];
        labelOfPrice.text = [NSString stringWithFormat:@"¥%@", [dicOfGood objectForKey:@"goods_price"]];
        labelOfPrice.font = [UIFont systemFontOfSize:28];
        [view addSubview:labelOfPrice];
        //运费
        UILabel *labelOfMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 470, 100, 30)];
        labelOfMoney.text = [NSString stringWithFormat:@"运费 %@ 元", [dicOfGood objectForKey:@"goods_storage_alarm"]];
        labelOfMoney.font = [UIFont systemFontOfSize:15];
        [view addSubview:labelOfMoney];
        //市场价
        UILabel *labelOfMarketPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 470, 100, 30)];
        labelOfMarketPrice.text = [NSString stringWithFormat:@"市场价 ¥%@", [dicOfGood objectForKey:@"goods_promotion_price"]];
        labelOfMarketPrice.font = [UIFont systemFontOfSize:15];
        labelOfMarketPrice.x_conterX = view.x_conterX;
        [view addSubview:labelOfMarketPrice];
        //月销量
        UILabel *labelOfMonthlySales = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 470, 100, 30)];
        labelOfMonthlySales.text = [NSString stringWithFormat:@"月销量%@件", [dicOfGood objectForKey:@"goods_salenum"]];
        labelOfMonthlySales.font = [UIFont systemFontOfSize:15];
        [view addSubview:labelOfMonthlySales];
        //viewOfLine
        UIView *viewOfLine1 = [[UIView alloc] initWithFrame:CGRectMake(0, 500, SCREEN_WIDTH, 1)];
        viewOfLine1.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
        [view addSubview:viewOfLine1];
        //viewOfBlue
        UIView *viewOfBlue = [[UIView alloc] initWithFrame:CGRectMake(0, 501, SCREEN_WIDTH, 80)];
        viewOfBlue.backgroundColor = [UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:0.081];
        [view addSubview:viewOfBlue];
        //
        UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 512, (SCREEN_WIDTH - 80) / 3, 60)];
        image1.image = [UIImage imageNamed:@"seven"];
        [view addSubview:image1];
        //
        UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(40 + (SCREEN_WIDTH - 80) / 3, 512, (SCREEN_WIDTH - 80) / 3, 60)];
        image2.image = [UIImage imageNamed:@"quality"];
        [view addSubview:image2];
        //
        UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(60 + (SCREEN_WIDTH - 80) / 3 * 2, 512, (SCREEN_WIDTH - 80) / 3, 60)];
        image3.image = [UIImage imageNamed:@"brand"];
        [view addSubview:image3];
        
        image1.layer.masksToBounds = YES;
        image1.layer.cornerRadius = 5;
        image2.layer.masksToBounds = YES;
        image2.layer.cornerRadius = 5;
        image3.layer.masksToBounds = YES;
        image3.layer.cornerRadius = 5;
        //viewOfLine
        UIView *viewOfLine2 = [[UIView alloc] initWithFrame:CGRectMake(0, 581, SCREEN_WIDTH, 1)];
        viewOfLine2.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
        [view addSubview:viewOfLine2];
        //like person
        UIView *viewOfLikePerson = [[UIView alloc] initWithFrame:CGRectMake(0, 582, SCREEN_WIDTH, 80)];
        for (int i = 0; i < 4; i++) {
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH / 2 - 60 * 4 / 2) + 60 * i, 10, 60, 60)];
            image.layer.masksToBounds = YES;
            image.layer.cornerRadius = 30;
            image.image = [UIImage imageNamed:@"1977blowout"];
            [viewOfLikePerson addSubview:image];
        }
        [view addSubview:viewOfLikePerson];
        return view;
    } else if(section == 1){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
        view.backgroundColor = [UIColor colorWithWhite:0.894 alpha:0.490];
        UIView *viewOfButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        viewOfButton.backgroundColor = [UIColor whiteColor];
        [view addSubview:viewOfButton];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH / 2, 40)];
        label.text = @"TA们是怎么说的 ~";
        label.textColor = [UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000];
        [viewOfButton addSubview:label];
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 5, 30, 30)];
        image.image = [UIImage imageNamed:@"comments"];
        [view addSubview:image];
        UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 5, 30, 30)];
        image1.image = [UIImage imageNamed:@"查看更多_1"];
        [view addSubview:image1];
        UILabel *labelOfStar = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 80, 40)];
        labelOfStar.textColor = [UIColor lightGrayColor];
        labelOfStar.text = @"综合评分";
        [view addSubview:labelOfStar];
        
        UIImageView *imageStar = [[UIImageView alloc] initWithFrame:CGRectMake(100, 50, 120, 20)];
        imageStar.image = [UIImage imageNamed:@"star"];
        [view addSubview:imageStar];
        
        UILabel *labelOfPercent = [[UILabel alloc] initWithFrame:CGRectMake(240, 45, 50, 30)];
        labelOfPercent.text = @"5.0分";
        labelOfPercent.textColor = [UIColor lightGrayColor];
        [view addSubview:labelOfPercent];
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 79, SCREEN_WIDTH, 1)];
        viewLine.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
        [view addSubview:viewLine];
        
        return view;
    } else if(section == 3) {
        NSDictionary *dicOfStore = model.store_info;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:dicOfStore[@"avatar"]]];
        view.backgroundColor = [UIColor whiteColor];
        [view addSubview:imageView];
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 20;
        UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 200, 20)];
        labelOfName.text = dicOfStore[@"store_name"];
        [view addSubview:labelOfName];
        
        UILabel *labelOfContent = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 20)];
        labelOfContent.text = dicOfStore[@"gc_name"];
        labelOfContent.textColor = [UIColor lightGrayColor];
        labelOfContent.font = [UIFont systemFontOfSize:12];
        [view addSubview:labelOfContent];
        
        _image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 10, 40, 40)];
        _image.image = [UIImage imageNamed:@"store"];
        [view addSubview:_image];
        _image.layer.masksToBounds = YES;
        _image.layer.cornerRadius = 20;
        _image.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapSaveStore = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSaveStore:)];
        [_image addGestureRecognizer:tapSaveStore];
        return view;
    } else if (section == 4){
        NSArray *arrayOfGuessLike = model.guess_like;
        if (arrayOfGuessLike.count == 1) {
            NSDictionary *first = arrayOfGuessLike[0];
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
            view.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 40)];
            label.text = @"为你推荐";
            [view addSubview:label];
            //左图
            UIView *viewOfLeft = [[UIView alloc] initWithFrame:CGRectMake(5, 60, (SCREEN_WIDTH - 15) / 2, 230)];
            [view addSubview:viewOfLeft];
            UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressLeft:)];
            [viewOfLeft addGestureRecognizer:tapLeft];
            //pic
            UIImageView *imageLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewOfLeft.frame.size.width, viewOfLeft.frame.size.width)];
            [imageLeft sd_setImageWithURL:[NSURL URLWithString:first[@"goods_image"]]];
            [viewOfLeft addSubview:imageLeft];
            UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(10, viewOfLeft.frame.size.width, viewOfLeft.frame.size.width, (viewOfLeft.frame.size.height - viewOfLeft.frame.size.width) / 2)];
            NSString *str = first[@"goods_name"];
            if (str.length > 8) {
                labelOfName.text = [str substringToIndex:8];
            } else {
                labelOfName.text = str;
            }
            labelOfName.textAlignment = 1;
            [viewOfLeft addSubview:labelOfName];
            
            UILabel *labelOfPrice = [[UILabel alloc] initWithFrame:CGRectMake(10, viewOfLeft.frame.size.width + labelOfName.frame.size.height, viewOfLeft.frame.size.width, (viewOfLeft.frame.size.height - viewOfLeft.frame.size.width) / 2)];
            labelOfPrice.text = [NSString stringWithFormat:@"¥ %@", first[@"goods_promotion_price"]];
            labelOfPrice.textAlignment = 1;
            [viewOfLeft addSubview:labelOfPrice];
            return view;
        } else {
        NSDictionary *first = arrayOfGuessLike[0];
        NSDictionary *last = arrayOfGuessLike[1];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 40)];
        label.text = @"为你推荐";
        [view addSubview:label];
        //左图
        UIView *viewOfLeft = [[UIView alloc] initWithFrame:CGRectMake(5, 60, (SCREEN_WIDTH - 15) / 2, 230)];
        [view addSubview:viewOfLeft];
        UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressLeft:)];
        [viewOfLeft addGestureRecognizer:tapLeft];
        //pic
        UIImageView *imageLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewOfLeft.frame.size.width, viewOfLeft.frame.size.width)];
        [imageLeft sd_setImageWithURL:[NSURL URLWithString:first[@"goods_image"]]];
        [viewOfLeft addSubview:imageLeft];
        UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(10, viewOfLeft.frame.size.width, viewOfLeft.frame.size.width, (viewOfLeft.frame.size.height - viewOfLeft.frame.size.width) / 2)];
        NSString *str = first[@"goods_name"];
        if (str.length > 8) {
            labelOfName.text = [str substringToIndex:8];
        } else {
            labelOfName.text = str;
        }
        labelOfName.textAlignment = 1;
        [viewOfLeft addSubview:labelOfName];
        
        UILabel *labelOfPrice = [[UILabel alloc] initWithFrame:CGRectMake(10, viewOfLeft.frame.size.width + labelOfName.frame.size.height, viewOfLeft.frame.size.width, (viewOfLeft.frame.size.height - viewOfLeft.frame.size.width) / 2)];
        labelOfPrice.text = [NSString stringWithFormat:@"¥ %@", first[@"goods_promotion_price"]];
        labelOfPrice.textAlignment = 1;
        [viewOfLeft addSubview:labelOfPrice];
        //右图
        UIView *viewOfRight = [[UIView alloc] initWithFrame:CGRectMake(10 + (SCREEN_WIDTH - 15) / 2, 60, (SCREEN_WIDTH - 15) / 2, 230)];
        [view addSubview:viewOfRight];
        UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressRight:)];
        [viewOfRight addGestureRecognizer:tapRight];
        
        UIImageView *imageRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewOfRight.frame.size.width, viewOfRight.frame.size.width)];
        [imageRight sd_setImageWithURL:[NSURL URLWithString:last[@"goods_image"]]];
        [viewOfRight addSubview:imageRight];
        UILabel *labelOfName1 = [[UILabel alloc] initWithFrame:CGRectMake(10, viewOfRight.frame.size.width, viewOfRight.frame.size.width, (viewOfRight.frame.size.height - viewOfRight.frame.size.width) / 2)];
        NSString *string = last[@"goods_name"];
        if (string.length > 8) {
            labelOfName1.text = [last[@"goods_name"] substringToIndex:8];
        } else {
            labelOfName1.text = string;
        }
        labelOfName1.textAlignment = 1;
        [viewOfRight addSubview:labelOfName1];
        
        UILabel *labelOfPrice1 = [[UILabel alloc] initWithFrame:CGRectMake(10, viewOfRight.frame.size.width + labelOfName1.frame.size.height, viewOfRight.frame.size.width, (viewOfRight.frame.size.height - viewOfRight.frame.size.width) / 2)];
        labelOfPrice1.text =[NSString stringWithFormat:@"¥ %@", last[@"goods_promotion_price"]];
        labelOfPrice1.textAlignment = 1;
        [viewOfRight addSubview:labelOfPrice1];
        
        return view;
        
        }
    } else {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc]initWithFrame:view.bounds];
        label.text = @"产品详情图";
        label.font = [UIFont systemFontOfSize:20];
        label.textColor = [UIColor lightGrayColor];
        [view addSubview:label];
        return view;
    }
    } else {
        return nil;
    }
}
//收藏店铺
- (void)handleSaveStore:(UITapGestureRecognizer *)tap
{
    ModelOfNewProductDetail *model = [_arrayOfData objectAtIndex:0];
    NSDictionary *dic = model.store_info;
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    NSString *password = [userDefault objectForKey:@"password"];
    if (name == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];
        NSLog(@"%@",name);
      [self presentViewController:login animated:YES completion:^{
          
      }];
        
    } else {
        NSString *body = [NSString stringWithFormat:@"key=%@&store_id=%@", password, dic[@"store_id"]];
        [ZWYNetTool POST:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=store_add" andBody:body andBodyStyle:ZWYBodyString andHeader:@{@"text/html": @"Content-Type",  @"utf-8" :@"charset"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            [_tableView reloadData];
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已收藏店铺" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            
            // 出现
            [self presentViewController:alertController animated:YES completion:^{
                
            }];
            // 移除
            [alertController dismissViewControllerAnimated:YES completion:^{
            }];
            _image.image = [UIImage imageNamed:@"savedStore"];
        } andFailureBlock:^(NSError *error) {
            
        }];
    }

}
- (void)pressLeft:(UITapGestureRecognizer *)tap {
if(_arrayOfData.count != 0) {
        ModelOfNewProductDetail *model = [_arrayOfData objectAtIndex:0];
    NSArray *arrayOfGuessLike = model.guess_like;
    NSDictionary *first = arrayOfGuessLike[0];
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = first[@"goods_id"];
    [self.navigationController pushViewController:new animated:YES];
    }
}
- (void)pressRight:(UITapGestureRecognizer *)tap {
    if(_arrayOfData.count != 0) {
        ModelOfNewProductDetail *model = [_arrayOfData objectAtIndex:0];
        NSArray *arrayOfGuessLike = model.guess_like;
        NSDictionary *last = arrayOfGuessLike[1];
        NewProductViewController *new = [[NewProductViewController alloc] init];
        new.goods_id = last[@"goods_id"];
        [self.navigationController pushViewController:new animated:YES];
    }
}
- (void)backOneNsstring:(NSIndexPath *)indexPath
{

}
//加入心愿单
- (void)handleWish:(UITapGestureRecognizer *)tap
{
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    NSString *password = [userDefault objectForKey:@"password"];
    if (name == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];

        [self presentViewController:login animated:YES completion:^{
            
        }];
    } else {
    NSString *body = [NSString stringWithFormat:@"goods_id=%@&key=%@", _goods_id, password];
    [ZWYNetTool POST:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=favorites_add" andBody:body andBodyStyle:ZWYBodyString andHeader:@{@"text/html": @"Content-Type",  @"utf-8" :@"charset"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        [_tableView reloadData];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已加入心愿单" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        // 出现
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
        // 移除
        [alertController dismissViewControllerAnimated:YES completion:^{
        }];
    
    } andFailureBlock:^(NSError *error) {
        
    }];
    }
}

- (void)back:(UIButton *)button
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
